#include "SavePointXml.h"
#include "Main/GateWay.h"
SavePointXml::SavePointXml()
{

}


bool SavePointXml::parser()
{
    m_SaveList.clear();
    m_SaveMap.clear();
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "PointData")   //解析Device
        {
            QDomNode PointChild = rootChild.toElement().firstChild();
            SavePointStruct savePointStruct;
            while(!PointChild.isNull())
            {
                if (PointChild.toElement().tagName() == "PointAddr")         //
                {
                    QString Addrdata = PointChild.toElement().text().remove(" ");
                    savePointStruct.setPointAddr(Addrdata.toUInt());

                }
                else if (PointChild.toElement().tagName() == "PointValue")    //
                {
                    QString Valuedata = PointChild.toElement().text().remove(" ");
                    savePointStruct.setPointValue(Valuedata.toUInt());
                }
                else if (PointChild.toElement().tagName() == "Time")    //
                {
                    QString Timedata = PointChild.toElement().text().remove(" ");
                    savePointStruct.setTime(Timedata);
                }
                PointChild = PointChild.nextSibling();
            }
            qDebug()<<"insert the key="<<savePointStruct.pointAddr();
            m_SaveMap.insert(savePointStruct.pointAddr(),savePointStruct);
            m_SaveList.append(savePointStruct.pointAddr());
        }
        rootChild=rootChild.nextSibling();
    }
    return true;
}
bool SavePointXml::modify(SavePointStruct savePointStruct)
{
    if(!this->check())
    {
        return false;
    }

    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root=doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "PointData")   //解析Device
        {
            QDomNodeList nodeList = rootChild.childNodes();
            for(int i=0; i<nodeList.size(); i++)
            {
                QDomNode node = nodeList.at(i);
                if(node.toElement().tagName() == "PointAddr")
                {
                    QString XMLaddr = node.toElement().text();
                    quint16 getdata=savePointStruct.pointAddr();
                    QString addr=QString::number(getdata,10);
                    if(XMLaddr==addr)
                    {
                        qDebug()<<"addr is old ,remove old="<<addr;

                       root.removeChild(rootChild);
                    }
                }
            }
        }
        rootChild=rootChild.nextSibling();
    }
    QDomElement PointListElement = doc.createElement("PointData");

    QDomText text;
    QDomElement addrElement = doc.createElement("PointAddr");
    quint16 a=savePointStruct.pointAddr();
    QString addr=QString::number(a,10);

    text = doc.createTextNode(addr);
    addrElement.appendChild(text);
    PointListElement.appendChild(addrElement);

    quint16 DataValue=savePointStruct.pointValue();

    QDomText Valuetext;
    QDomElement ValueElement = doc.createElement("PointValue");
    QString value = QString::number(DataValue,10).toUpper() ;

    Valuetext = doc.createTextNode(value);
    ValueElement.appendChild(Valuetext);
    PointListElement.appendChild(ValueElement);

    QDomText Datatext;
    QDomElement TimeElement = doc.createElement("Time");
    QString data = savePointStruct.getTime();
    qDebug()<<"Time="<<data;
    Datatext = doc.createTextNode(data);
    TimeElement.appendChild(Datatext);
    PointListElement.appendChild(TimeElement);


    root.appendChild(PointListElement);

    file.open(QFile::WriteOnly|QFile::Truncate); //先读进来，再重写，如果不用truncate就是在后面追加内容，就无效了
    QTextStream outStream(&file);
    doc.save(outStream,4); //缩进4格
    file.flush();
    file.close();
 //   parser();
    return true;
}
bool SavePointXml::clear()
{
    if(!this->clearAllNode())
    {
        return false;
    }

    m_SaveList.clear();
    return true;
}


void SavePointXml::joinMap(quint16 addr,SavePointStruct PointStruct)
{
    m_SaveMap.insert(addr,PointStruct);
}
void SavePointXml::cutMap(quint16 addr)
{
    m_SaveMap.remove(addr);
}
bool SavePointXml::findData(quint16 addr)
{
    if(m_SaveMap.contains(addr)==true)
    {
        return true;
    }
    else
    {
        return false;
    }
}
void SavePointXml::appendList(quint16 addr)
{
    m_SaveList.append(addr);

}



QMap<quint16,SavePointStruct> SavePointXml::getMap()
{
    return m_SaveMap;
}
QList<quint16>SavePointXml::getList()
{
    return m_SaveList;
}

void SavePointXml::setSynchroTime(QString time)
{
    m_SynchroTime=time;
}
QString SavePointXml::getSynchroTime()
{
    return m_SynchroTime;
}

void SavePointXml::SavePointData(quint16 addr,quint16 value)
{
    saveTurnStata(true);
    SavePointStruct pointData;
    pointData.setPointAddr(addr);
    pointData.setPointValue(value);
    QDateTime systemTime(QDateTime::currentDateTime());
    QString strTime = systemTime.toString("[yyyy-MM-dd hh:mm:ss]");
    pointData.setTime(strTime);
    if(findData(addr)==true)
    {
        joinMap(addr,pointData);
        qDebug()<<"oldjoin-----address="<<addr<<"  value="<<pointData.pointValue();
        qDebug()<<"Time="<<pointData.getTime();
 //       savePointXml->modify(pointData);
    }
    else
    {
        joinMap(addr,pointData);
        appendList(addr);
        qDebug()<<"newjoin-----address="<<addr<<"  value="<<pointData.pointValue();
        qDebug()<<"Time="<<pointData.getTime();
    }

}

bool SavePointXml::TurnStata()
{
    return m_TurnStata;
}
void SavePointXml::saveTurnStata(bool stata)
{
    m_TurnStata=stata;
}

bool SavePointXml::AutomaticContrast(quint16 addr,quint16 value)
{
    qDebug()<<"your addra ="<<addr;
    if(m_SaveMap.contains(addr)==true)
    {
        SavePointStruct Contrastdata= m_SaveMap.value(addr);
        if(Contrastdata.pointValue()==value)
        {
            return true;
        }
        else
        {
            qDebug()<<"not right,value="<<Contrastdata.pointValue();
            qDebug()<<"but your value="<<value;
            return false;
        }
    }
    else
    {
        qDebug()<<"not have value";
        return false;
    }
}

quint16 SavePointXml::GetSavePointValue(quint16 addr)
{
    qDebug()<<"your addra ="<<addr;
    SavePointStruct Contrastdata= m_SaveMap.value(addr);
    return Contrastdata.pointValue();

}

void SavePointXml::JoinSystemList(quint16 addr,quint16 value,QString stata)
{
    SavePointStruct systemPointStruct;
    systemPointStruct.setPointAddr(addr);
    systemPointStruct.setPointValue(value);
    systemPointStruct.setStata(stata);
    QDateTime systemTime(QDateTime::currentDateTime());
    QString strTime = systemTime.toString("[yyyy-MM-dd hh:mm:ss]");
    systemPointStruct.setTime(strTime);
    m_systemList.append(systemPointStruct);
}
QList<SavePointStruct>SavePointXml::getSystemList()
{
    return m_systemList;
}
void SavePointXml::clearnSystemList()
{
    m_systemList.clear();
}

quint16 SavePointXml::getsavetime()
{
    return m_saveTime;
}
void SavePointXml::clernsavetime()
{
    m_saveTime=0;
}
void SavePointXml::timeup()
{
 m_saveTime++;
}
